//////////////////////////////////////////////////////////////////////
// PointerCast.h
//
// SHEN Fangyang
// me@shenfy.com
//
// Copyright (C) SHEN Fangyang, 2011, All rights reserved.
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
//  Brief Description
//
//	Customized smart pointer construction and casting
//
//////////////////////////////////////////////////////////////////////
#pragma once
#include <boost/smart_ptr.hpp>
//#ifndef BOOST_MEM_FN_ENABLE_STDCALL
#define BOOST_MEM_FN_ENABLE_STDCALL
//#endif
#include <boost/mem_fn.hpp>

namespace grflib
{
    namespace engine
    {
        namespace core
        {
            namespace cast
            {
                template<typename T>
                inline boost::shared_ptr<T> GetSharedPtr(T* p)
                {
                    return boost::shared_ptr<T>(p);
                }

                inline void CallCOMRelease(IUnknown *p) {if (p) {p->Release(); p = 0;}}

                template<typename T>
                inline boost::shared_ptr<T> GetCOMPtr(T* p)
                {
                    return boost::shared_ptr<T>(p, CallCOMRelease);
                }

                template<typename T>
                inline boost::shared_ptr<IResource> IResourceCast(boost::shared_ptr<T> &p)
                {
                    return boost::dynamic_pointer_cast<IResource>(p);
                }

                template<typename T>
                inline boost::shared_ptr<IRenderable> IRenderableCast(boost::shared_ptr<T> &p)
                {
                    return boost::dynamic_pointer_cast<IRenderable>(p);
                }

                template<typename T>
                inline boost::shared_ptr<CSceneObj> CSceneObjCast(boost::shared_ptr<T> &p)
                {
                    return boost::dynamic_pointer_cast<CSceneObj>(p);
                }

                template<typename T>
                inline boost::shared_ptr<IInteractive> IInteractiveCast(boost::shared_ptr<T> &p)
                {
                    return boost::dynamic_pointer_cast<IInteractive>(p);
                }

            }
        } //namespace core
    } //namespace engine
} //namespace grflib